package com.github.postsanf.yinian.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;

public class MemoryCardGuideActivity extends Activity implements View.OnClickListener {
    private MediaPlayer mediaPlayer;
    private TextView mcard_guide_play_time;
    private ImageView lineplay;
    private ImageView mcardg_use;
    private ImageView mcardg_cancel;
    private AnimationDrawable anima;
    private SimpleDraweeView image;
    private boolean IstoSync;
    boolean bb = false;

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_memory_card_guide);
        initView();
    }

    private void initView() {
        IstoSync = getIntent().getBooleanExtra(ACTIONs.IstoSync, false);
        image = (SimpleDraweeView) findViewById(R.id.card_guide_qg);
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inSampleSize = 1;
//        image.setImageBitmap(BitmapFactory.decodeResource(getResources(),
//                R.drawable.memory_card_guide_qg, options));
        ImageLoader.getInstance().displayImage("drawable://" + R.drawable.memory_card_guide_qg,
                image);
        ((TextView) findViewById(R.id.mcardg_content_txt)).setTypeface(Typeface.createFromAsset(this.getAssets(), "fzqk.ttf"));
        ((TextView) findViewById(R.id.mcardg_date_year)).setTypeface(Typeface.createFromAsset(this.getAssets(), "fzqk.ttf"));
        ((TextView) findViewById(R.id.mcardg_date_mouth)).setTypeface(Typeface.createFromAsset(this.getAssets(), "fzqk.ttf"));
        ((TextView) findViewById(R.id.mcardg_date_ri)).setTypeface(Typeface.createFromAsset(this.getAssets(), "fzqk.ttf"));
        mcard_guide_play_time = (TextView) findViewById(R.id.mcard_guide_play_time);
        lineplay = (ImageView) this.findViewById(R.id.mcard_guide_audio_play);
        mcardg_use = (ImageView) this.findViewById(R.id.mcardg_use);
        mcardg_cancel = (ImageView) this.findViewById(R.id.mcardg_cancel);
        mcardg_cancel.setOnClickListener(this);
        mcardg_use.setOnClickListener(this);
        findViewById(R.id.mcard_guide_play).setOnClickListener(this);
        anima = (AnimationDrawable) lineplay
                .getDrawable();
        anima.setOneShot(false);
        anima.stop();
        anima.selectDrawable(2);
        mediaPlayer = new MediaPlayer();
//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
        try {
            mediaPlayer.setDataSource(getString(R.string.MEMORY_CARD_AUDIO));
            mediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
//                mHandler.sendEmptyMessage(1);
//            }
//        }.start();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                              @Override
                                              public void onPrepared(MediaPlayer mp) {
                                                  int time = mp.getDuration() / 1000;
                                                  mcard_guide_play_time.setText(time + "''");
                                                  bb = true;
                                              }
                                          }
        );
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()

                                            {
                                                @Override
                                                public void onCompletion(MediaPlayer mp) {
                                                    anima.stop();
                                                    anima.selectDrawable(2);
                                                }
                                            }

        );
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mcardg_cancel:
                finish();
                break;
            case R.id.mcardg_use:
                startActivity(new Intent(MemoryCardGuideActivity.this, LocalMemoryCardManageActivity.class).
                        putExtra(ACTIONs.IstoSync, IstoSync));
                finish();
                mediaPlayer.stop();
                break;
            case R.id.mcard_guide_play:
                if (bb = false)
                    return;
                if (mediaPlayer == null)
                    return;
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    anima.stop();
                } else {
                    mediaPlayer.start();
                    anima.start();
                }
                break;
        }
    }

    @Override
    protected void onPause() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            anima.stop();
        }
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }


    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }


    @Override
    protected void onDestroy() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            anima.stop();
        }
        mediaPlayer.release();
        mediaPlayer = null;
        super.onDestroy();
    }
}
